package com.ocom.human.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.entity.human.EntityMsgYkmConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.human.AddMsgYkmConfigVo;
import com.ocom.human.service.MsgYkmConfigService;
//import com.ocom.human.vo.AddMsgYkmConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MsgYkmConfigController {

    @Autowired
    private MsgYkmConfigService msgYkmConfigService;

    /**
     * 服务消息推送列表（getMsgYkmConfigList）
     * */
    @GetMapping(HumanServiceUrl.URL_GetMsgYkmConfigList)
    public Result GetMsgYkmConfigList(Long comId){
        if (comId == null){
            log.error("服务消息推送列表 comId 为空");
            return ResultUtil.error(ResultEnum.Err_203,"服务消息推送列表 comId 为空");
        }
        return ResultUtil.success(msgYkmConfigService.GetMsgYkmConfigList(comId));
    }

    /**
     * 服务消息新增（addMsgYkmConfig）
     * */
    @PostMapping(HumanServiceUrl.URL_AddMsgYkmConfig)
    public Result AddMsgYkmConfig(@Validated @RequestBody Map<String, Object> map){
        log.info("服务消息新增请求参数：{{}}",map);
        Long comId = Long.valueOf(map.get("comId").toString());
        List<AddMsgYkmConfigVo> addMsgYkmConfigVos = JSON.parseArray(map.get("params").toString(), AddMsgYkmConfigVo.class);
        List<EntityMsgYkmConfig> objects = new ArrayList<>();
        for (AddMsgYkmConfigVo addMsgYkmConfigVo:addMsgYkmConfigVos) {
            EntityMsgYkmConfig entityMsgYkmConfig = new EntityMsgYkmConfig();
            entityMsgYkmConfig.setComId(comId);
            entityMsgYkmConfig.setTrigGer(addMsgYkmConfigVo.getTriggerId());
            entityMsgYkmConfig.setRoles(addMsgYkmConfigVo.getRoles());
            entityMsgYkmConfig.setSendChannels(addMsgYkmConfigVo.getSendChannels());
            entityMsgYkmConfig.setFreeTimeSlot(addMsgYkmConfigVo.getFreeTime());
            objects.add(entityMsgYkmConfig);
        }
        try {
            msgYkmConfigService.AddMsgYkmConfig(comId,objects);
        }catch (Exception exception){
            log.info("服务消息新增失败 {{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"服务消息新增失败");
        }
        return ResultUtil.success();
    }
}
